package cn.itlaobing.web.servlet;

import cn.itlaobing.exception.MyBusinessException;
import cn.itlaobing.model.OrderModel;
import cn.itlaobing.model.UserModel;
import cn.itlaobing.service.OrderService;
import cn.itlaobing.web.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by lhb on 2017/11/3.
 */
@WebServlet("/order/*")
public class OrderServlet extends BaseServlet{
    @RequestMapping("/del")
    public void del(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException {
        String count=request.getParameter("DEL");
        OrderService orderService=new OrderService();
        try {
            orderService.del(count);
            response.sendRedirect("/order/show");
            return;
        } catch (MyBusinessException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/quxiao")
    public void quxiao(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException {
        String count=request.getParameter("QUXIAO");
        System.out.println(count);
        OrderService orderService=new OrderService();
        try {
            orderService.quxiao(count);
            response.sendRedirect("/order/show");
            return;
        } catch (MyBusinessException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/zhifu")
    public void zhifu(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException {
        String count=request.getParameter("ACCOUNT");
        OrderService orderService=new OrderService();
        try {
            orderService.zhifu(count);
            response.sendRedirect("/order/show");
            return;
        } catch (MyBusinessException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/show")
    public void show(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        List<OrderModel> list = null;
        OrderService os=new OrderService();
        try {
            UserModel um=(UserModel)request.getSession().getAttribute("USER");
            list=os.findAll(um.getUserid());
        } catch (MyBusinessException e) {
            e.printStackTrace();
        }
        request.setAttribute("SHOW",list);
        if(list!=null) {
            request.getRequestDispatcher("/WEB-INF/detail/show.jsp").forward(request, response);
        }else {
            request.getRequestDispatcher("/WEB-INF/error/error.jsp").forward(request, response);
        }
    }
}
